package com.fanghaoran.view.user.bookFrame;

import com.fanghaoran.view.user.BackGroundPanel;
import com.fanghaoran.entity.Words;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * @Author: win11
 * @Date: 2023/03/18/22:10
 * @Description: 背单词的界面
 */
public class TestFrame extends JFrame {
    public TestFrame(Words word) throws Exception {

        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView(word);

        //设置可见
        this.setVisible(true);
    }

    private void initView(Words word) throws Exception {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //用垂直box实现组件垂直排列
        Box vBox=Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));

        //单词图标
        JLabel wordLabel =new JLabel(word.getWord());
        //选项按钮
        JButton yes=new JButton("认识");
        JButton no=new JButton("不认识");
        //添加到box
        vBox.add(wordLabel);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(yes);
        vBox.add(no);
        //添加到panel
        panel.add(vBox);
        //添加到frame
        this.add(panel);

        //添加点击事件
        //认识按钮
        yes.addActionListener(e-> {
                try {
                    //关闭当前页面
                    exit();
                    //点击后跳转到答案界面
                    new AnswerFrame(word);
                }catch (Exception exception) {
                    exception.printStackTrace();
                }
                }
        );
        //不认识按钮
        no.addActionListener(e-> {
                try {
                    //关闭当前页面
                    exit();
                    //点击后跳转到答案页面
                    new AnswerFrame(word);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                }
        );
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    /**
     * 退出当前页面(点击事件重写方法中)
     */
    private void exit(){
        this.dispose();
    }
}
